package com.bang.sgg.service;

import com.bang.sgg.persist.model.AgentSeat;
import com.bang.sgg.persist.model.mapper.AgentSeatMapperExt;
import com.bang.sgg.persist.model.mapper.AgentsMapperExt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

/**
 * @Auther: Bang
 * @Date: 2019/11/21 17:11
 * @Description:
 */
@Service
@Slf4j
//@CacheConfig(cacheNames = "agent")//公共配置
public class HelloService {

    @Autowired
    private AgentSeatMapperExt agentSeatMapperExt;

    @Cacheable(cacheNames = "agent", key = "#id")
    public AgentSeat get(Long id) {
        log.info("=============== 开始查询坐席信息 id=[{}]", id);
        AgentSeat agentSeat = agentSeatMapperExt.selectByPrimaryKey(id);
        return agentSeat;
    }


    public AgentSeat add(AgentSeat agentSeat) {
        log.info("=============== 开始插入坐席信息 agentSeat=[{}]", agentSeat);
        int i = agentSeatMapperExt.insertSelective(agentSeat);
        return agentSeat;
    }

    // 更新缓存,在方法执行后

    @CachePut(cacheNames = "agent",key = "#result.id")
    public AgentSeat update(AgentSeat agentSeat) {
        log.info("=============== 开始更新坐席信息 agentSeat=[{}]", agentSeat);
        int i = agentSeatMapperExt.updateByPrimaryKeySelective(agentSeat);
        return agentSeat;
    }

    // 清空指定id缓存
    // 是否在方法执行前就清空，缺省为 false，如果指定为 true，
    // 则在方法还没有执行的时候就清空缓存，
    //缺省情况下，如果方法执行抛出异常，则不会清空缓存
    @CacheEvict(cacheNames = "agent",key = "#id",beforeInvocation = false)
    public int delete(Long id) {
        log.info("=============== 开始删除坐席信息 id=[{}]", id);
        int i = agentSeatMapperExt.deleteByPrimaryKey(id);
        return i;
    }

    // 清空所有缓存
    @CacheEvict(cacheNames = "agent",allEntries = true)
    public int deleteAll(Long id) {
        log.info("=============== 开始删除坐席信息 id=[{}]", id);
        int i = agentSeatMapperExt.deleteByPrimaryKey(id);
        return i;
    }

    @Caching(
           cacheable = {
                   @Cacheable(cacheNames = "agent",key = "#id")
           },
            put = {
                   @CachePut(cacheNames = "agent",key = "#result.extCode"),
                   @CachePut(cacheNames = "agent",key = "#result.seatCode"),
            }
    )
    public AgentSeat getAll(Long id) {
        log.info("=============== 开始删除坐席信息 id=[{}]", id);
        AgentSeat agentSeat = agentSeatMapperExt.selectByPrimaryKey(id);
        return agentSeat;
    }


    @Cacheable(value = "userStatistics", key = "#id")
    public AgentSeat selectById(Long id) {
        log.info("=============== 开始删除坐席信息 id=[{}]", id);
        AgentSeat agentSeat = agentSeatMapperExt.selectByPrimaryKey(id);
        return agentSeat;
    }

}
